#include "gvj.h"
#include "handler_poll.h"
#include "poj_handler.h"
#include "poj_task.h"

#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <cstdio>
#include <cstring>
#include <python2.7/Python.h>

int main() {
	init_python();
	const char *cookie_dir = "/root/project/GVJ/cookie/";
	const char *src_dir = "/root/project/GVJ/test_src/poj/";
	HandlerPoll<PojHandler, PojTask> *hp = 
		new HandlerPoll<PojHandler, PojTask>(5, 10, 1234, 1235, cookie_dir);
	hp->start();

	/*
	while (true) {
		char pid[10];
		char file[100];
		const char *language = "G++";
		char source[10000];

		printf("input the problem id: ");
		scanf("%s", pid);
		if (!strcmp(pid, "exit")) break;
		sprintf(file, "%s%s.cpp", src_dir, pid);
		
		int fd = open(file, O_RDONLY);
		if (fd < 0) {
			printf("can not open file");
			continue;
		}
		ssize_t rn = read(fd, source, 10000);
		ssize_t wn = write(1, source, rn);

		PojTask task("", "", "", "poj", pid, language, source);
		hp->add_task(task);
	}
	*/


	for (int i = 0; i < 10; i++) {
		char pid[10];
		char file[100];
		const char *language = "G++";
		char source[10000];

		//printf("input the problem id: ");
		//scanf("%s", pid);
		sprintf(pid, "1008");
		if (!strcmp(pid, "exit")) break;
		sprintf(file, "%s%s.cpp", src_dir, pid);
		
		int fd = open(file, O_RDONLY);
		if (fd < 0) {
			printf("can not open file");
			continue;
		}
		ssize_t rn = read(fd, source, 10000);
		//ssize_t wn = write(1, source, rn);

		PojTask task("", "", "", "poj", pid, language, source);
		hp->add_task(task);
	}

	sleep(100);
	//hp->stop();
	finalize_python();
	delete hp;
}
